define(['jquery','vue','common'],function ($,Vue,common) {
    const Controller = {
        index:function (){
            let {createApp} = Vue;
            let app = createApp({
                data(){
                    return {
                        menuVisible:false,
                        xialaxuanzPostion:{
                            x:99,
                            y:120
                        },
                        menuItems:[],//菜单
                        contract:false,//是否收缩
                        collapse:false,//转换风格
                        userInfo:Config.adminInfo,//用户信息
                        tabs:{
                            list:[],//tabs列表
                            activeTab:0,//当前选中的tabs
                            keys:[]
                        },
                        //  主题
                        theme:{
                            showChangeTheme:false,//显示修改主题
                            theme_color:""
                        },
                        Config:Config,
                        activeMenu:0,
                        temUserInfo:Config.temUserInfo,
                    }
                },
                created(){
                    // console.log(this.userInfo)
                        this.getMenu(true)
                    window.updateMenu = this.getMenu
                    window.getUserInfo = this.getUserInfo
                    if(Config?.base?.theme_color){
                        this.theme.theme_color = Config.base.theme_color
                        this.onSubmitTemp()
                    }
                    window.my_changeTheme = this.my_changeTheme
                },
                methods:{
                    ...common,
                    onSubmit(){
                        let theme_color = this.theme.theme_color;
                        let url = common.getHandleUrl('change_theme_color')
                        let that = this;
                        common.postHandle(url,{theme_color:theme_color},true).then(res=>{
                            that.success("修改成功",()=>{
                                that.theme.showChangeTheme = false
                                that.onSubmitTemp()
                            })
                        })
                    },
                    /**
                     * 修改主题
                     * @param theme_color
                     */
                    my_changeTheme(theme_color){
                        this.theme.theme_color = theme_color
                        this.onSubmitTemp()
                    },
                    onSubmitTemp(){
                        // console.log(this.theme.theme_color)
                        let  theme_color = common.copy(this.theme.theme_color)
                        common.changeTheme(theme_color)
                        for(let key=0;key<this.tabs.keys.length;key++){
                            let ids = `myid_`+this.tabs.keys[key]
                            // console.log(`myid_`+this.tabs.keys[key])
                            let iframe = document.getElementById(ids);
                            console.log(iframe.contentWindow)
                            iframe.contentWindow.postMessage({
                                action:'changeTheme',
                                data:theme_color
                            })
                        }
                    },
                    addUserInfo(){
                        this.addMain(this.temUserInfo)
                    },
                    getUserInfo(){
                        let url = '/app/muadmin/admin/index/get_user_info'
                        let that = this
                        common.getHandle(url).then(res=>{
                            that.userInfo = res
                        })
                    },
                    addDizi(){
                        let list = this.tabs.list;
                        let activeTab = this.tabs.activeTab
                        let route = ''
                        $.each( list,function (index,item){
                            if(item.key==activeTab){
                                route = item.route
                            }
                        })
                        route = route.split('?')
                        route = route[0]
                        // console.log(location.href)
                        let newroute = location.href
                        newroute =  newroute.split('#')
                         newroute = newroute[0]
                        newroute = newroute+'#'+route
                        history.pushState(null,null,newroute)
                        // console.log(route,'rrrrrrrrrrrrr')
                    },
                    //退出登录
                    logout(){
                        common.post('/app/muadmin/admin/index/logout').then(res=>{
                            if(res.code==1){
                                common.message(res.msg,'success',function (){
                                    window.location.href = '/app/muadmin/admin/index/login'
                                })
                            }else{
                                common.message(res.msg,'error')
                            }
                        })
                    },
                    //关闭tabs
                    closeTab(id){
                        let index = this.tabs.list.findIndex(item=>item.key==id)
                        let xzthis = this.tabs.activeTab==id?true:false
                        let indexKeys  = this.tabs.keys.findIndex(item=>item==id)
                        console.log(indexKeys)
                        this.tabs.list.splice(index,1)
                        this.tabs.keys.splice(indexKeys,1)
                        if(xzthis){
                            if(this.tabs.list[index-1]){
                                this.tabs.activeTab = this.tabs.list[index-1].key
                                this.activeMenu = this.tabs.list[index-1].key
                            }else if(this.tabs.list[index]){
                                this.tabs.activeTab = this.tabs.list[index].key
                                this.activeMenu = this.tabs.list[index].key
                            }
                        }
                        this.addDizi()
                    },
                    openMenu(event,label){
                        console.log(event,label)
                        // this.xialaxuanzPostion.x = event.clientX
                        // this.xialaxuanzPostion.y = event.clientY
                        // this.$refs.xialaxuanz.handleOpen(event)
                    },
                    //选择tabs
                    changeTabs(id){
                        this.activeMenu = id
                        this.addDizi()
                    },
                    //添加tabs
                    addMain(row){
                        // console.log(row)
                        if(row.route){
                            if(!this.tabs.keys.includes(row.id)){
                                let route = `/app/${row.plugin}/${row.module}/${row.route}`
                                if(route.indexOf('?')>-1){
                                    route = route+'&ref=addtabs'
                                }else{
                                    route = route+'?ref=addtabs'
                                }
                                this.tabs.list.push({
                                    label:row.title,
                                    route:route,
                                    icon:row.icon,
                                    key:row.id
                                })
                                this.tabs.keys.push(row.id)
                                this.tabs.activeTab  = row.id
                            }else{
                                this.tabs.activeTab  = row.id
                            }
                            if(this.activeMenu!=row.id){
                                this.activeMenu = row.id
                            }
                            this.addDizi()
                        }
                    },
                    addRefresh(){
                        if(Config.refresh){
                            this.addMain(Config.refresh)
                        }
                        console.log(Config,'config')
                    },
                    // 获取菜单
                    getMenu(getRefresh=false){
                        let that = this;
                        common.get('/app/muadmin/admin/index/menu').then(res=>{
                            // console.log(res)
                            if(res.code==1){
                                that.menuItems = res.data
                                if(that.activeMenu==0){
                                    $.each(res.data,function (index,item){
                                        if(item.id==1){
                                            that.addMain(item)
                                        }
                                    })
                                }
                                if(getRefresh===true){
                                    that.addRefresh();
                                }

                            }
                        })
                    }
                }
            })
            common.load(app,['MenuItem','icon'])
            app.mount('#app')
        },
        login:function (){
            $("#submit").click(function (){
                let forms = $("#form").serializeArray()
                let data = {}
                $.each(forms,function (index,item){
                    data[item.name] = item.value
                })
                common.post('/app/muadmin/admin/index/login',{row:data},true).then(res=>{
                    if(res.code==1){
                        common.message(res.msg,'success',function (){
                            if(res.url){
                                window.location.href = res.url
                            }else{
                                window.location.href = '/app/muadmin/admin/index/index'
                            }
                        })
                    }else{
                        common.message(res.msg,'error')
                    }
                })
                console.log(data)
            })
            // let {createApp} = Vue;
            // let app = createApp({
            //     data(){
            //         return {
            //             //登录表单
            //             loginForm:{
            //                 username:"",
            //                 password:""
            //             }
            //         }
            //     },
            //     methods:{
            //         // 登录
            //         onSubmit(){
            //             if(!this.loginForm.username){
            //                 common.message('请输入用户名')
            //                 return
            //             }
            //             if(!this.loginForm.password){
            //                 common.message('请输入密码')
            //                 return
            //             }
            //             common.post('/app/muadmin/admin/index/login',{row:this.loginForm},true).then(res=>{
            //                 if(res.code==1){
            //                     common.message(res.msg,'success',function (){
            //                         if(res.url){
            //                             window.location.href = res.url
            //                         }else{
            //                             window.location.href = '/app/muadmin/admin/index/index'
            //                         }
            //                     })
            //                 }else{
            //                     common.message(res.msg,'error')
            //                 }
            //             })
            //         }
            //     }
            // })
            // common.load(app)
            // app.mount('#app')
        }
    }
    return Controller;
})